﻿using System.Data.Entity;
using System.Threading;
using Wisdom.Core.Repositories;


namespace Wisdom.Repositories.EntityFramework
{
    /// <summary>
    /// 描述：Entity Framework仓库上下文
    /// </summary>
    public class EntityFrameworkRepositoryContext : RepositoryContext, IEntityFrameworkRepositoryContext
    {
        #region Private Fields
        private readonly ThreadLocal<DbContext> localContext = new ThreadLocal<DbContext>();
        #endregion

        #region Ctor
        /// <summary>
        /// 初始化 <c>EntityFrameworkRepositoryContext</c> 类.
        /// </summary>
        /// <param name="efContext">类 <see cref="DbContext"/> 实例被 <c>EntityFrameworkRepositoryContext</c> 类实例初始化.</param>
        public EntityFrameworkRepositoryContext(DbContext efContext)
        {
            this.localContext.Value = efContext;
        }
        #endregion

        /// <summary>
        /// 注册一个删除聚合根到仓库上下文中
        /// </summary>
        /// <typeparam name="TAggregateRoot">指定的聚合根类型</typeparam>
        /// <param name="obj">聚合根实例</param>
        public override void RegisterDeleted<TAggregateRoot>(TAggregateRoot obj)
        {
            localContext.Value.Entry(obj).State = System.Data.EntityState.Deleted;
            Committed = false;
        }
        /// <summary>
        /// 注册一个修改聚合根到仓库上下文中.
        /// </summary>
        /// <typeparam name="TAggregateRoot">指定的聚合根类型</typeparam>
        /// <param name="obj">聚合根实例</param>
        public override void RegisterModified<TAggregateRoot>(TAggregateRoot obj)
        {
            localContext.Value.Entry(obj).State = System.Data.EntityState.Modified;
            Committed = false;
        }
        /// <summary>
        /// 册一个新增聚合根到仓库上下文中.
        /// </summary>
        /// <typeparam name="TAggregateRoot">指定的聚合根类型</typeparam>
        /// <param name="obj">聚合根实例</param>
        public override void RegisterNew<TAggregateRoot>(TAggregateRoot obj)
        {
            localContext.Value.Entry(obj).State = System.Data.EntityState.Added;
            Committed = false;
        }
        /// <summary>
        /// 提交上下文操作
        /// </summary>
        public override void Commit()
        {
            if (!Committed)
            {
                localContext.Value.SaveChanges();
                Committed = true;
            }
        }
        /// <summary>
        /// 回滚上下文操作
        /// </summary>
        public override void Rollback()
        {
            Committed = false;
        }

        protected override void DisposeCore()
        {
            if (!Committed)
                Commit();
            localContext.Value.Dispose();
            localContext.Dispose();
            base.DisposeCore();
        }

        #region IEntityFrameworkRepositoryContext Members
        /// <summary>
        /// Entity Framework操作上下文
        /// </summary>
        public DbContext Context
        {
            get { return localContext.Value; }
        }

        #endregion
    }
}
